package com.efast.cafe.portal.service.common.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import com.efast.cafe.framework.redis.ObjectRedisTemplate;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.common.EntityInfoBean;
import com.efast.cafe.portal.dao.common.EntityInfoQueryDao;
import com.efast.cafe.portal.service.common.IEntityInfoQueryService;
import com.efast.cafe.util.ConfigUtil;
import com.efast.cafe.util.StringUtil;

@RestController
@Transactional
public class EntityInfoQueryServiceImpl implements IEntityInfoQueryService {

	private EntityInfoQueryDao queryDao;
	@Autowired
	private ObjectRedisTemplate objectRedisTemplate;

	@Autowired
	public void setQueryDao(EntityInfoQueryDao queryDao) {
		this.queryDao = queryDao;
	}

	@Override
	public EntityInfoBean queryEntityInfo(String entityName) {

		ValueOperations<String,Object> redisValueObj = objectRedisTemplate.opsForValue();
		EntityInfoBean ent=(EntityInfoBean) redisValueObj.get(entityName);
		if(ent==null){
			ent = queryDao.queryEntityInfo(entityName, SpringWebUtils.getRequestCompany(),ConfigUtil.getSystemVersion(), ConfigUtil.getSystemIndentifier());
			if(ent!=null){
				if(StringUtil.isNotBlank(ent.getInherit())){
					ent.setParentEntityInfo(queryDao.queryEntityInfo(ent.getInherit(), SpringWebUtils.getRequestCompany(), ConfigUtil.getSystemVersion(), ConfigUtil.getSystemIndentifier()));
				}
				redisValueObj.set(entityName, ent);
			}
			
			
		}
		return ent;
	}

	@Override
	public EntityInfoBean queryEntityInfoByTableName(String tableName) {
		ValueOperations<String,Object> redisValueObj = objectRedisTemplate.opsForValue();
		EntityInfoBean ent=(EntityInfoBean) redisValueObj.get(tableName);
		if(ent==null){
		    ent = queryDao.queryEntityInfoByTableName(tableName, SpringWebUtils.getRequestCompany(), ConfigUtil.getSystemVersion(), ConfigUtil.getSystemIndentifier());
		    if(ent!=null){
				if(StringUtil.isNotBlank(ent.getInherit())){
					ent.setParentEntityInfo(queryDao.queryEntityInfo(ent.getInherit(), SpringWebUtils.getRequestCompany(), ConfigUtil.getSystemVersion(), ConfigUtil.getSystemIndentifier()));
				}
				redisValueObj.set(tableName, ent);
			}
		}
		return ent;
	}

}
